<?php

/***
 * Class OperateController
 * 工作运营用户
 */
class OperateController extends ManageController
{

    function init()
    {
        parent::init();
    }

    function indexaction()
    {
        $this->_layout->meta_title = '用户管理';
        $condition = array();
        $page = isset($_REQUEST['page']) ? addslashes(htmlspecialchars(intval($_REQUEST['page']))) : 1;

        $page_size = 20;
        $admin_user_model = new AdminUserModel();
        $data = $admin_user_model->getListData($page, $page_size);
        #分页处理
        $totalnum = $admin_user_model->getListCount();
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->user_rowsets = $data;
    }

    function createAction()
    {
        $admin_user_model = new AdminUserModel();

        #提交信息
        if ($this->getRequest()->isPost()) {

            $data = array(
                'username' => isset($_REQUEST['username']) ? addslashes(htmlspecialchars(trim($_REQUEST['username']))) : '',
                'email' => isset($_REQUEST['email']) ? addslashes(htmlspecialchars(trim($_REQUEST['email']))) : '',
                'password' => isset($_REQUEST['password']) ? addslashes(htmlspecialchars(trim($_REQUEST['password']))) : '',
            );
            $ret = $admin_user_model->addData($data);
            if($ret){
                $this->set_popup_message('添加用户成功');
            }else{
                $this->set_popup_message('添加用户失败');
            }
            $this->redirect('/manage/operate/index');
            return False;
        }

        $this->_layout->meta_title = '创建后台用户';
    }


    function roleAction()
    {
        $uid = isset($_REQUEST['uid']) ? addslashes(htmlspecialchars(intval($_REQUEST['uid']))) : 0;
        if ($uid <= 0) {
            $this->set_popup_message('用户信息未选择');
            $this->redirect('/manage/operate/index');
            return False;
        }

        $admin_role_model = new AdminRolesModel();
        $admin_user_model = new AdminUserModel();
        $userinfo = $admin_user_model->getDataByUid($uid);  //获取后台用户信息
        if (empty($userinfo)) {
            $this->set_popup_message('用户信息未选择');
            $this->redirect('/manage/operate/index');
            return False;
        }

        #提交修改
        if ($this->getRequest()->isPost()) {
            #删除全部权限
            $admin_role_model->deleteByUid($uid);
            $aids = !empty($_REQUEST['aids']) ? addslashes(htmlspecialchars($_REQUEST['aids'])) : array();

            #插入所有权限
            $admin_role_model->addAllData($uid, $aids);

            $this->set_popup_message("后台用户权限修改成功");
            $this->redirect('/manage/operate/index/');
            return false;
        }

        #查询全部权限
        $accesslist = array();
        $allaccesslist = $admin_role_model->getAllAccessList();
        foreach ($allaccesslist as $val) {
            $accesslist[$val['id']] = $val;
        }

        #该用户的权限
        $roleaccesslist = $admin_role_model->getListByUid($uid);
        foreach ($roleaccesslist as $val) {
            $accesslist[$val['access_id']]['checked'] = 1;
        }

        $this->_view->userinfo = $userinfo;
        $this->_view->accesslist = $accesslist;
        $this->_layout->meta_title = '修改后台用户权限';
    }
}
?>